(function flexible(window, document) {
    const appContainer = document.querySelector('.indexPage');
    let isFullScreenMode = false;
    let designWidth = 2230; // 默认非全屏尺寸
    let designHeight = 1250; // 默认非全屏尺寸
    const baseFontSize = 37.5;
    let preFullscreenFontSize = 0;

    function resetFontSize() {
        const devicePixelRatio = window.devicePixelRatio || 1;
        // 使用更可靠的屏幕尺寸获取方式
        const screenWidth = isFullScreenMode ? window.screen.width : window.innerWidth;
        const screenHeight = isFullScreenMode ? window.screen.height : window.innerHeight;

        // 根据全屏状态切换设计尺寸
        if (isFullScreenMode) {
            designWidth = 1920;
            designHeight = 1080;
        } else {
            designWidth = 2230;
            designHeight = 1250;
        }

        let fontSize;
        if (isFullScreenMode && appContainer) {
            const widthRatio = screenWidth / designWidth;
            const heightRatio = screenHeight / designHeight;
            const scaleRatio = Math.min(widthRatio, heightRatio);

            // 限制缩放比例范围，避免极端情况
            const minScale = 0.5;
            const maxScale = 2;
            const validScaleRatio = Math.max(minScale, Math.min(maxScale, scaleRatio));

            fontSize = validScaleRatio * baseFontSize;
            appContainer.style.transform = `scale(${validScaleRatio})`;
            appContainer.style.transformOrigin = 'center'; // 修改为居中缩放
            appContainer.style.width = designWidth / devicePixelRatio + 'px';
            appContainer.style.height = designHeight / devicePixelRatio + 'px';
            appContainer.style.position = 'absolute'; // 确保容器绝对定位
            appContainer.style.top = '50%';
            appContainer.style.left = '50%';
            appContainer.style.transform = `translate(-50%, -50%) scale(${validScaleRatio})`;
        } else {
            fontSize = (document.documentElement.clientWidth / designWidth) * baseFontSize;
            if (appContainer) {
                appContainer.style.transform = '';
                appContainer.style.width = '';
                appContainer.style.height = '';
                appContainer.style.position = '';
                appContainer.style.top = '';
                appContainer.style.left = '';
            }
        }

        document.documentElement.style.fontSize = fontSize + 'px';
        if (!isFullScreenMode) {
            preFullscreenFontSize = fontSize;
        }
    }

    function handleFullscreenChange() {
        // 统一的全屏状态判断函数
        function isInFullScreen() {
            return document.fullscreenElement || 
                   document.webkitFullscreenElement || 
                   document.msFullscreenElement;
        }
        
        isFullScreenMode = isInFullScreen();
        resetFontSize();
    }

    window.addEventListener('pageshow', resetFontSize);
    window.addEventListener('resize', resetFontSize);
    document.addEventListener('fullscreenchange', handleFullscreenChange);
    document.addEventListener('webkitfullscreenchange', handleFullscreenChange);
    document.addEventListener('MSFullscreenChange', handleFullscreenChange);

    resetFontSize();

    window.setFullScreenMode = function (mode) {
        isFullScreenMode = mode;
        resetFontSize();
    };
})(window, document);